/********************************************************************
	Rhapsody	: 7.5 
	Login		: KBE
	Component	: DefaultComponent 
	Configuration 	: CodeConfig
	Model Element	: Reactor
//!	Generated Date	: Fri, 7, Sep 2012  
	File Path	: DefaultComponent\CodeConfig\Reactor.cpp
*********************************************************************/

//## auto_generated
#include "Reactor.h"
//## dependency SimulatorReactorImplementation
#include "SimulatorReactorImplementation.h"
//itsReactorImplementation->registerHanler(h, eh, et);

//## package Default

//## class Reactor
Reactor* Reactor::reactor_impl_;

void Reactor::handleEvents(int time) {
    //#[ operation handleEvents(int)
    //#]
}

Reactor * Reactor::instance() {
    //#[ operation instance()
    if (reactor_impl_ == 0)
    	reactor_impl_ = new SimulatorReactorImplementation();   
    return reactor_impl_;
    //#]
}

void Reactor::registerHandler(EventHandler& eh, const Event_Type& dt) {
    //#[ operation registerHandler(EventHandler,Event_Type)
    addItsEventHandler(dt, &eh);  
    addItsEHANDLE(eh.getHandle());
    //#]
}

void Reactor::removeHandler(EventHandler& eh, const Event_Type& et) {
    //#[ operation removeHandler(EventHandler,Event_Type)
    //#]
}

Reactor::Reactor() : itsEventHandler() {
    //#[ operation Reactor()
    reactor_impl_ = 0;
    
    //#]
}

void Reactor::registerHandler(EHANDLE& h, EventHandler& eh, const Event_Type& et) {
    //#[ operation registerHandler(EHANDLE,EventHandler,Event_Type)
     addItsEventHandler(et, &eh);  
     addItsEHANDLE(&h);
     EHANDLE *handle = eh.getHandle();
     
    //#]
}

void Reactor::removeHandler(EHANDLE& h, const Event_Type& et) {
    //#[ operation removeHandler(EHANDLE,Event_Type)
    //#]
}

Reactor::~Reactor() {
    cleanUpRelations();
}

void Reactor::setReactor_impl_(Reactor* p_reactor_impl_) {
    reactor_impl_ = p_reactor_impl_;
}

std::map<Event_Type, EventHandler*>::const_iterator Reactor::getItsEventHandler() const {
    std::map<Event_Type, EventHandler*>::const_iterator iter;
    iter = itsEventHandler.begin();
    return iter;
}

std::map<Event_Type, EventHandler*>::const_iterator Reactor::getItsEventHandlerEnd() const {
    return itsEventHandler.end();
}

void Reactor::removeItsEventHandler(EventHandler* p_EventHandler) {
    if(p_EventHandler != NULL)
        {
            p_EventHandler->__setItsReactor(NULL);
        }
    _removeItsEventHandler(p_EventHandler);
}

void Reactor::clearItsEventHandler() {
    std::map<Event_Type, EventHandler*>::const_iterator iter;
    iter = itsEventHandler.begin();
    while (iter != itsEventHandler.end()){
        ((*iter).second)->_clearItsReactor();
        iter++;
    }
    _clearItsEventHandler();
}

void Reactor::cleanUpRelations() {
    {
        itsEHANDLE.clear();
    }
    {
        std::map<Event_Type, EventHandler*>::const_iterator iter;
        iter = itsEventHandler.begin();
        while (iter != itsEventHandler.end()){
            Reactor* p_Reactor = ((*iter).second)->getItsReactor();
            if(p_Reactor != NULL)
                {
                    ((*iter).second)->__setItsReactor(NULL);
                }
            iter++;
        }
        itsEventHandler.clear();
    }
}

void Reactor::_removeItsEventHandler(EventHandler* p_EventHandler) {
    std::map<Event_Type, EventHandler*>::iterator pos = std::find_if(itsEventHandler.begin(), itsEventHandler.end(),OMValueCompare<const Event_Type,EventHandler*>(p_EventHandler));
    if (pos != itsEventHandler.end()) {
    	itsEventHandler.erase(pos);
    }
}

void Reactor::_clearItsEventHandler() {
    itsEventHandler.clear();
}

std::list<EHANDLE*>::const_iterator Reactor::getItsEHANDLE() const {
    std::list<EHANDLE*>::const_iterator iter;
    iter = itsEHANDLE.begin();
    return iter;
}

std::list<EHANDLE*>::const_iterator Reactor::getItsEHANDLEEnd() const {
    return itsEHANDLE.end();
}

void Reactor::addItsEHANDLE(EHANDLE* p_EHANDLE) {
    itsEHANDLE.push_back(p_EHANDLE);
}

void Reactor::removeItsEHANDLE(EHANDLE* p_EHANDLE) {
    std::list<EHANDLE*>::iterator pos = std::find(itsEHANDLE.begin(), itsEHANDLE.end(),p_EHANDLE);
    if (pos != itsEHANDLE.end()) {
    	itsEHANDLE.erase(pos);
    }
}

void Reactor::clearItsEHANDLE() {
    itsEHANDLE.clear();
}

EventHandler* Reactor::getItsEventHandler(Event_Type key) const {
    return (itsEventHandler.find(key) != itsEventHandler.end() ? (*itsEventHandler.find(key)).second : NULL);
}

void Reactor::removeItsEventHandler(Event_Type key) {
    EventHandler* p_EventHandler = getItsEventHandler(key);
    if(p_EventHandler != NULL)
        {
            p_EventHandler->_setItsReactor(NULL);
        }
    _removeItsEventHandler(key);
}

void Reactor::_removeItsEventHandler(Event_Type key) {
    itsEventHandler.erase(key);
}

void Reactor::addItsEventHandler(Event_Type key, EventHandler* p_EventHandler) {
    if(p_EventHandler != NULL)
        {
            p_EventHandler->_setItsReactor(this);
        }
    _addItsEventHandler(key, p_EventHandler);
}

void Reactor::_addItsEventHandler(Event_Type key, EventHandler* p_EventHandler) {
    itsEventHandler.insert(std::map<Event_Type, EventHandler*>::value_type(key, p_EventHandler));
}

/*********************************************************************
	File Path	: DefaultComponent\CodeConfig\Reactor.cpp
*********************************************************************/
